package com.ykdl.app.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;
import cn.jpush.android.api.JPushInterface;

import com.androidquery.AQuery;
import com.litesuits.http.LiteHttp;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.JsonRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.ykdl.app.R;
import com.ykdl.app.Utils.AccessTokenKeeper;
import com.ykdl.app.base.BaseActivity;
import com.ykdl.app.bean.RegisterPhonebean;
import com.ykdl.app.common.MyApplication;
import com.ykdl.app.connect.Constants;
import com.ykdl.app.main.MainActivity;

public class LoginActivity extends BaseActivity {

	private AQuery mAQ;
	private MyApplication application;
	private boolean is_Chock = false;
	private AccessTokenKeeper mAccessTokenKeeper;
	private LiteHttp client;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		changeSystemBarToWhite();
		setLeftShowOrHidden(false);
		mAQ = new AQuery(this);
		application = (MyApplication) this.getApplication();
		initView();
	}

	private void initView() {
		
		mAccessTokenKeeper = new AccessTokenKeeper(this);
		client = MyApplication.getInstance().getLiteHttp();
		
		if (is_Chock) {
			mAQ.id(R.id.iv_Express).getImageView()
					.setBackgroundResource(R.drawable.login_display);
		} else {
			mAQ.id(R.id.iv_Express).getImageView()
					.setBackgroundResource(R.drawable.login_hide);
		}
		// 密码明文密文显示
		mAQ.id(R.id.iv_Express).clicked(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!is_Chock) {
					is_Chock = true;
					mAQ.id(R.id.iv_Express).getImageView()
							.setBackgroundResource(R.drawable.login_display);
					mAQ.id(R.id.tv_password)
							.getEditText()
							.setTransformationMethod(
									HideReturnsTransformationMethod
											.getInstance());
				} else {
					is_Chock = false;
					mAQ.id(R.id.iv_Express).getImageView()
							.setBackgroundResource(R.drawable.login_hide);
					mAQ.id(R.id.tv_password)
							.getEditText()
							.setTransformationMethod(
									PasswordTransformationMethod.getInstance());
				}
			}
		});

		EditText edit = (EditText) findViewById(R.id.tv_password);

		edit.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_DONE) {
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(mAQ.id(R.id.tv_password)
							.getEditText().getWindowToken(), 0);
					login();
				}
				return true;
			}
		});

		// 找回密码
		mAQ.id(R.id.tv_forget_pwd).clicked(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(LoginActivity.this, ForgetPwdActivity.class);
			}
		});
		// 登录
		mAQ.id(R.id.login).clicked(new OnClickListener() {

			@Override
			public void onClick(View v) {
				login();
			}

		});
	}

	public void login() {
		if (mAQ.id(R.id.tv_phone).getText().toString().isEmpty()) {
			Toast.makeText(getApplicationContext(), "请输入手机号", 0).show();
			return;
		} else if (mAQ.id(R.id.tv_phone).getText().toString().length() != 11) {
			Toast.makeText(getApplicationContext(), "请输入正确的手机号码", 0).show();
			return;
		} else if (mAQ.id(R.id.tv_password).getText().toString().isEmpty()) {
			Toast.makeText(getApplicationContext(), "请输入密码", 0).show();
			return;
		} else if (mAQ.id(R.id.tv_password).getText().toString().length() < 6) {
			Toast.makeText(getApplicationContext(), "请输入不少于6位的密码", 0).show();
			return;
		}

		if (!isConnectInternet()) {
			Toast.makeText(getApplicationContext(), "网络连接不畅，请检查网络设置", 0).show();
			return;
		}
		setIsShowLoad(true);
		LoginHttp();
	}

	private void LoginHttp() {
		// TODO Auto-generated method stub

		String urls = "";//NetConfig.LOGIN;
		JsonRequest<RegisterPhonebean> cacheRequest1 = new JsonRequest<RegisterPhonebean>(
				urls, RegisterPhonebean.class);
		cacheRequest1.addUrlParam("client_id", Constants.CLIENT_ID);
		cacheRequest1.addUrlParam("client_secret", Constants.CLIENT_SECRET);
		cacheRequest1.addUrlParam("identity", mAQ.id(R.id.tv_phone).getText()
				.toString());
		cacheRequest1.addUrlParam("password", mAQ.id(R.id.tv_password)
				.getText().toString());

		cacheRequest1.addUrlParam("grant_type", "password");
		cacheRequest1.setMethod(HttpMethods.Post);
		System.out.println(cacheRequest1);
		cacheRequest1.setHttpListener(new HttpListener<RegisterPhonebean>() {
			@Override
			public void onSuccess(RegisterPhonebean user,
					Response<RegisterPhonebean> response) {
				setIsShowLoad(false);
				if (user.getError() != null) {
					
					Toast.makeText(getApplication(), user.getDesc(), 1).show();

				} else {
                   //存账号和密码
					saveSharedPreferencesValue(Constants.SH_PHONE,
							mAQ.id(R.id.tv_phone).getText().toString());
					saveSharedPreferencesValue(Constants.SH_PASSWORD,
							mAQ.id(R.id.tv_password).getText().toString());
					mAccessTokenKeeper
							.keepAccessToken(LoginActivity.this, user);
					application.syncAccessToken();    //同步token
//					JPushInterface.resumePush(LoginActivity.this);
					startActivity(LoginActivity.this, MainActivity.class);
					Toast.makeText(LoginActivity.this, "登录成功", 1).show();
					LoginActivity.this.finish();
				}
			}

			@Override
			public void onFailure(HttpException e,
					Response<RegisterPhonebean> response) {
				setIsShowLoad(false);
				// Toast.makeText(getApplication(), "", 1).show();
			}
		});
		client.executeAsync(cacheRequest1);

	}

}
